package net.yla.smartbus.module.controller.web.ba.equipment;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.core.util.ExcelUtils;
import net.yla.smartbus.module.ba.equipment.dto.DeviceDto;
import net.yla.smartbus.module.ba.equipment.form.DeviceForm;
import net.yla.smartbus.module.ba.equipment.mapper.DeviceMapper;
import net.yla.smartbus.module.ba.equipment.query.DeviceQuery;
import net.yla.smartbus.module.ba.equipment.serice.DeviceService;
import net.yla.smartbus.module.ba.vehicle.common.ExcelData;
import net.yla.smartbus.module.ba.vehicle.utils.MyExcelUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 默苍离
 * @date 2021/6/22 10:10
 * <p>
 * ******************************************
 * @annotation:
 */
@RestController
@RequestMapping("/api/web/taxi/ba")
public class DeviceController extends BaseController {

    public DeviceController(DeviceService service, DeviceMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    private final DeviceService service;

    private final DeviceMapper mapper;

    /**
     * 根据查询条件获取 所有 设备信息
     *
     * @param query
     * @return
     */
    @PostMapping("/device/getList")
    public ApiResult getList(@RequestBody DeviceQuery query) {
        PageBean<DeviceDto> list = service.getListByPage(query);
        return success(list);
    }


    /**
     * 根据设备id删除设备信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/device/delete")
    public ApiResult delete(@RequestBody Integer[] ids) {
        mapper.delete(ids);
        return success();
    }


    /**
     * 下载excel模板
     */
    @PostMapping("/device/exportTemplate")
    @CrossOrigin
    public ApiResult exportTemplate(HttpServletResponse response) throws IOException, InvalidFormatException {
        String path = "/exportTemplate/设备导入模板.xls";
        String fileName = "设备导入模板";
        ExcelUtils.exportTemplate(response, path, fileName);

        return success(success());
    }


    /**
     * 导出设备信息
     */
    @GetMapping("/device/exportDevice")
    public ApiResult exportVehicles(HttpServletResponse response, DeviceQuery query) throws IOException {
        ExcelData excelData = service.exportDevice(query);
        MyExcelUtil.exportExcel(response, excelData);
        return success(success());
    }


    /**
     * 导入车辆信息
     */
    @PostMapping("/device/importDevice")
    public ApiResult importVehicles(MultipartFile file) throws IOException {
        service.insertByImport(file);
        return success(success());
    }


    /**
     * 新增、修改车辆
     */
    @PostMapping("/device/addEquipment")
    public ApiResult addEquipment(@RequestBody DeviceForm form) {

        if (form.getDeviceId() != null) {
            return success(service.update(form));
        } else {
            Integer insert = service.insert(form);
            return success(insert);
        }

    }


    /**
     * 根据 设备id 查询 一台 设备信息
     */
    @PostMapping("/device/getOneById")
    public ApiResult getOneById(Integer deviceId) {
        DeviceDto oneById = service.getOneById(deviceId);
        return success(oneById);
    }


}
